home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-25 | 3.8 KB | 125 lines | [TEXT/CWIE] |
- // ===========================================================================
- // NS_Utils.cp ©1996 Timo Eloranta
- // ===========================================================================
- // Utility functions used by the NeuroSim app
-
- #include "NS_Utils.h"
- #include <Sound.h> // Universal header - SndPlay() etc.
-
- // ---------------------------------------------------------------------------
- // • RangedRdm
- //
- // Called by: CNeuralNet::GenerateConnections
- // CNeuralNet::GetRandomReceptor
- // ---------------------------------------------------------------------------
- // Return a random short integer from the range [inMin, inMax].
-
- Int16
- RangedRdm( Int16 inMin, Int16 inMax)
- {
- Int16 theRange = (inMax - inMin) + 1;
- Int32 theRaw = ::Random();
-
- if ( theRaw < 0L )
- theRaw *= -1L;
-
- theRaw = ( theRaw * (Int32) theRange ) / ( (Int32) max_Int16 + 1L );
-
- return inMin + (Int16) theRaw;
- }
-
- // ---------------------------------------------------------------------------
- // • PlayOneSnd
- //
- // Called by: CNeuron::IncState
- // CStdNeuron::DoClickAction
- // ---------------------------------------------------------------------------
- // Play a single sound from a snd resource. If the Boolean inAsync
- // parameter is true, the sound is played asynchronously.
-
- pascal OSErr
- PlayOneSnd ( ResIDT inSoundID, Boolean inAsync)
- {
- OSErr theOE = noErr;
-
- static SndListHandle sPlayingSound;
- static SndChannelPtr sCurChannel;
-
- if ( sCurChannel ) {
- if (!( theOE = ::SndDisposeChannel( sCurChannel, true )))
- sCurChannel = nil;
- }
-
- if ( sPlayingSound ) {
- OSErr theOE2;
- ::ReleaseResource( (Handle) sPlayingSound );
- if ( !( theOE2 = ::ResError() ))
- sPlayingSound = nil;
- else if ( !theOE )
- theOE = theOE2;
- }
-
- if ( !theOE ) {
- sPlayingSound = (SndListHandle) ::GetResource('snd ', inSoundID );
-
- if ( !( theOE = ::ResError() ))
- if ( !sPlayingSound )
- theOE = resNotFound;
- else {
- ::HLockHi( (Handle) sPlayingSound );
-
- if ( !inAsync ) {
- if (!(theOE = ::SndPlay( nil, sPlayingSound, false ))) {
- ::ReleaseResource( (Handle) sPlayingSound );
- if ( !theOE )
- theOE = ::ResError ( );
- if ( !theOE )
- sPlayingSound = nil;
- }
- }
- else if (!(theOE = ::SndNewChannel( &sCurChannel,
- sampledSynth, initMono, nil)))
-
- theOE = ::SndPlay( sCurChannel, sPlayingSound, true );
- }
- }
- return theOE;
- }
-
- // ---------------------------------------------------------------------------
- // • SignedIntField
- //
- // Called by: CParamsDialog::InitDialog
- // ---------------------------------------------------------------------------
- // Key Filter for Integer characters plus the '-' character
- //
- // > Identify delete and cursor keys
- // > Accept the '-' character
- // > Accept numbers (0 to 9)
- // > Reject all other printing characters
- // > PassUp all other characters
-
- EKeyStatus
- SignedIntField( const EventRecord &inKeyEvent)
- {
- EKeyStatus theKeyStatus = keyStatus_PassUp;
- Char16 theKey = inKeyEvent.message;
- Char16 theChar = theKey & charCodeMask;
-
- if (UKeyFilters::IsTEDeleteKey(theKey)) {
- theKeyStatus = keyStatus_TEDelete;
- } else if (UKeyFilters::IsTECursorKey(theKey)) {
- theKeyStatus = keyStatus_TECursor;
- } else if (UKeyFilters::IsExtraEditKey(theKey)) {
- theKeyStatus = keyStatus_ExtraEdit;
- } else if (UKeyFilters::IsPrintingChar(theChar)) {
- if (UKeyFilters::IsNumberChar(theChar) || theChar == '-') {
- theKeyStatus = keyStatus_Input;
- } else {
- theKeyStatus = keyStatus_Reject;
- }
- }
-
- return theKeyStatus;
- }
-